const app = getApp();
Page({

  data: {
    typeList: [],
    indexGoodsList: [],
    typeGoodsList: [],
    currentType: 0
  },
  onLoad: function (options) {
    this.getCategory();
    this.getIndexGoodsList();
  },
  onShow: function () {
  },
  getCategory() {
    wx.request({
      url: `${app.globalData.baseurl}category/`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            typeList: res.data
          });
          console.log(this.data.typeList);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  getIndexGoodsList() {
    wx.request({
      url: `${app.globalData.baseurl}category/${this.data.currentType + 1}`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            typeGoodsList: res.data
          });
          console.log(this.data.typeGoodsList);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  getTypeGoodsList(event) {
    // console.log(event.currentTarget.dataset.id);
    // console.log(event.currentTarget.dataset.index);
    let index = event.currentTarget.dataset.index;
    let id = event.currentTarget.dataset.id;
    this.setData({
      currentType: index
    })
    wx.request({
      url: `${app.globalData.baseurl}category/${id}`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            typeGoodsList: res.data
          });
          console.log(this.data.typeGoodsList);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
  },
  toGoodDetail(event) {
    console.log(event.currentTarget.dataset.id);
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../goodDetail/goodDetail?goodsId=' + id,
    })
  }

})